package com.yuexunit.mobilenurse.module.SignQuery.xml;

import android.util.Xml;

import com.yuexunit.mobilenurse.module.SignQuery.bean.SignQueryBean;
import com.yuexunit.mobilenurse.module.SignQuery.bean.SignQueryErrBean;

import org.xmlpull.v1.XmlPullParser;

import java.io.ByteArrayInputStream;
import java.util.ArrayList;

/**
 * Created by work-jx on 2017/2/8.
 */
public class SignQueryXML {
    public static SignQueryErrBean getSignQuery(String s)
    {
        if(s == null) {
            return null;
        }
        XmlPullParser parser = Xml.newPullParser();
        String time = null;
        SignQueryErrBean signQueryErrBean = null;
        ArrayList<SignQueryBean> signQueryBeans = null;
        SignQueryBean signQueryBean = null;
        ByteArrayInputStream tInputStringStream = null;
        try
        {
            if (s != null && !s.trim().equals("")) {
                tInputStringStream = new ByteArrayInputStream(s.getBytes());
            }
        }
        catch (Exception e) {
        }
        try
        {
            parser.setInput(tInputStringStream,"utf-8");

            int type = parser.getEventType();
            while(type != XmlPullParser.END_DOCUMENT)
            {
                switch (type)
                {
                    case XmlPullParser.START_TAG:
                        if("Body".equals(parser.getName()))
                        {
                        }else if("Response".equals(parser.getName()))
                        {
                            signQueryErrBean = new SignQueryErrBean();
                            signQueryErrBean.setErrMsg(parser.getAttributeValue(null,"ErrMsg"));
                            signQueryBeans = new ArrayList<SignQueryBean>();
                        }
                        else if("PATIENTS".equals(parser.getName()))
                        {
                            time = new String();
                        }
                        else if("MEASURE_POINT".equals(parser.getName()))
                        {
                            time =parser.nextText();
                        }
                        else if ("TARGETS".equals(parser.getName()))
                        {
                            signQueryBean = new SignQueryBean();
                        }
                        else if("VITAL_SIGNS".equals(parser.getName()))
                        {
                            signQueryBean.setTimes(time);
                            signQueryBean.setName(parser.nextText());
                        }
                        else if ("VITAL_SIGNS_VALUE".equals(parser.getName()))
                        {
                            signQueryBean.setValue(parser.nextText());
                            signQueryBeans.add(signQueryBean);
                        }
                        break;
                    case XmlPullParser.END_TAG:
                        if("Body".equals(parser.getName()))
                        {
                            signQueryErrBean.setSignQueryBeans(signQueryBeans);
                        }
                        break;
                    default:
                        break;
                }
                type = parser.next();
            }
        } catch (Exception e)
        {
            e.printStackTrace();
        }
        return signQueryErrBean;
    }
}
